<template>
  <div class="mod-log">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="用户名／用户操作" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.type" placeholder="日志类型" @change="getDataList()">
          <el-option v-for="item in typeList" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="dataList" border v-loading="dataListLoading" style="width: 100%;max-height: calc(100vh - 300px);">
      <el-table-column prop="id" header-align="center" align="center" width="80" label="ID">
      </el-table-column>
      <el-table-column prop="username" header-align="center" align="center" label="用户名">
      </el-table-column>
      <el-table-column prop="operation" header-align="center" align="center" label="用户操作">
      </el-table-column>
      <el-table-column prop="method" header-align="center" align="center" width="250" :show-overflow-tooltip="true"
        label="请求方法">
      </el-table-column>
      <el-table-column prop="params" header-align="center" align="center" width="250" :show-overflow-tooltip="true"
        label="请求参数">
      </el-table-column>
      <el-table-column prop="time" header-align="center" align="center" label="执行时长(毫秒)" width="150">
      </el-table-column>
      <el-table-column prop="result" header-align="center" align="center" label="请求结果" width="250"
        :show-overflow-tooltip="true">
      </el-table-column>
      <el-table-column prop="ip" header-align="center" align="center" width="150" label="IP地址">
      </el-table-column>
      <el-table-column prop="createDate" header-align="center" align="center" width="180" label="创建时间">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="batch(scope.row)">
            查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

    <el-dialog :title="titles" :visible.sync="dialogFormVisibleRw" center width="70%">

      <el-form ref="form" :model="form" label-width="150px">
        <el-form-item label="ID：">
          <div>{{form.id}}</div>
        </el-form-item>
        <el-form-item label="用户名：">
          <div>{{form.username}}</div>
        </el-form-item>
        <el-form-item label="用户操作：">
          <div>{{form.method}}</div>
        </el-form-item>
        <el-form-item label="请求方法：">
          <div>{{form.operation}}</div>
        </el-form-item>
        <el-form-item label="请求参数：">
          <el-input type="textarea" placeholder="请输入内容" v-model="form.params" :rows="3">
          </el-input>
        </el-form-item>
        <el-form-item label="执行时长(毫秒)：">
          <div>{{form.time}}</div>
        </el-form-item>
        <el-form-item label="请求结果：">
          <el-input type="textarea" placeholder="请输入内容" v-model="form.result" :rows="3">
          </el-input>
        </el-form-item>
        <el-form-item label="IP地址：">
          <div>{{form.ip}}</div>
        </el-form-item>
        <el-form-item label="创建时间：">
          <div>{{form.createDate}}</div>
        </el-form-item>
      </el-form>
    </el-dialog>



  </div>
</template>

<script>
  export default {
    data() {
      return {
        dataForm: {
          key: '',
          type: 1
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        selectionDataList: [],
        titles: '',
        dialogFormVisibleRw: false,
        form: {
          createDate: undefined,
          id: undefined,
          ip: undefined,
          method: undefined,
          operation: undefined,
          params: undefined,
          time: undefined,
          username: undefined,
          result: undefined
        },
        typeList: [
          { label: '管理端日志', value: 1 },
          { label: '移动端日志', value: 2 }
        ]
      }
    },
    created() {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList() {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/sys/log/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key,
            'type': this.dataForm.type
          })
        }).then(({ data }) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },


      //查看
      batch(row) {
        this.titles = '查看日志'
        this.dialogFormVisibleRw = true
        this.form = Object.assign({}, row)
      },



      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      }
    }
  }
</script>
<style scoped>
  .params_item {
    white-space: pre-wrap;
    word-break: break-all;
  }
</style>